/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Mathern
 */
public class RssChannelModelDAO implements java.io.Serializable, IHibernate {

    private static ArrayList<RssChannelModelDAO> list;

    /**
     * @return the list
     */
    public static ArrayList<RssChannelModelDAO> getList() {
        return list;
    }

    /**
     * @param aList the list to set
     */
    public static void setList(ArrayList<RssChannelModelDAO> aList) {
        list = aList;
    }
    
    private String title = null;
    private String link = null;
    private String desc = null;
    private List<RssItemModelDAO> channelItems = null;
    private String lang = null;
    private String docs = null;
    private String webmasterMail = null;
    private String datePub = null;
    private ArrayList<RssItemModelDAO> plist;

    /**
     * 
     */
    public RssChannelModelDAO() {
    }

    //constructeur de recopie
    /**
     * 
     * @param obj
     */
    public RssChannelModelDAO(RssChannelModelDAO obj) {
        title = obj.getTitle();
        link = obj.getLink();
        desc = obj.getDesc();
        channelItems = obj.getChannelItems();
        lang = obj.getLang();
        docs = obj.getDocs();
        webmasterMail = obj.getWebmasterMail();
        datePub = obj.getDatePub();
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }

    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the desc
     */
    public String getDesc() {
        return desc;
    }

    /**
     * @param desc the desc to set
     */
    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * @return the channelItems
     */
    public List<RssItemModelDAO> getChannelItems() {
        return channelItems;
    }

    /**
     * @param channelItems the channelItems to set
     */
    public void setChannelItems(List<RssItemModelDAO> channelItems) {
        this.channelItems = channelItems;
    }

    /**
     * @return the lang
     */
    public String getLang() {
        return lang;
    }

    /**
     * @param lang the lang to set
     */
    public void setLang(String lang) {
        this.lang = lang;
    }

    /**
     * @return the docs
     */
    public String getDocs() {
        return docs;
    }

    /**
     * @param docs the docs to set
     */
    public void setDocs(String docs) {
        this.docs = docs;
    }

    /**
     * @return the webmasterMail
     */
    public String getWebmasterMail() {
        return webmasterMail;
    }

    /**
     * @param webmasterMail the webmasterMail to set
     */
    public void setWebmasterMail(String webmasterMail) {
        this.webmasterMail = webmasterMail;
    }

    /**
     * @return the datePub
     */
    public String getDatePub() {
        return datePub;
    }

    /**
     * @param datePub the datePub to set
     */
    public void setDatePub(String datePub) {
        this.datePub = datePub;
    }

    /**
     * 
     * @param id
     */
    public void setId(String id) {
        this.link = id;
    }

    /**
     * 
     * @return
     */
    public String getId() {
        return this.link;
    }

    /**
     * 
     */
    @Override
    public void saveObject() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.saveOrUpdate(this);

        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    @Override
    public void create() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(this);

        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    @Override
    public void read() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        //On doit créer une query HQL pour ramener tous les éléments de la table
        //On utilise le langage integre, HQL
        //Query query = session.createQuery("from RssItemModelDAO where id="+id);
        Query query = session.createQuery("from RssChannelModelDAO where id=?").setString(0, link);
        plist = (ArrayList) query.list();

        /*
         * //foreach de parcours de liste for(RssItemModelDAO elt : list){
         * System.out.println(elt.getId()); }
        //
         */
        session.getTransaction().commit();
        session.close();
    }

    /**
     * 
     */
    @Override
    public void update() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.update(this);

        session.getTransaction().commit();
        session.close();
    }

    /**
     * 
     */
    @Override
    public void delete() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.delete(this);

        session.getTransaction().commit();
        session.close();
    }

    /**
     * 
     */
    public static void loadObjects() {
        //initialisation de la liste "lazy"
        Hibernate.initialize(list);
        
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        //On doit créer une query HQL pour ramener tous les éléments de la table
        //On utilise le langage integre, HQL
        Query query = session.createQuery("from RssChannelModelDAO");
        setList((ArrayList<RssChannelModelDAO>) (ArrayList) query.list());

        session.getTransaction().commit();
        session.close();
    }
    
    public ArrayList<RssChannelModelDAO> loadOneObject(String linkChosen) {
        
        ArrayList<RssChannelModelDAO> theList=null;
        
        //initialisation de la liste "lazy"
        Hibernate.initialize(theList);
        
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        //On doit créer une query HQL pour ramener tous les éléments de la table
        //On utilise le langage integre, HQL
        Query query = session.createQuery("from RssChannelModelDAO where id=?").setString(0, linkChosen);
        theList = ((ArrayList<RssChannelModelDAO>) (ArrayList) query.list());

        session.getTransaction().commit();
        session.close();
        
        return theList;
    }
}
